package cn.gzsf.springboot04.controller;

import cn.gzsf.springboot04.entity.Emp;
import cn.gzsf.springboot04.mapper.EmpMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class EmpController {
    //自动装配注解，此注解会在创建一个EmpMapper接口的实现类，并实例化一个对象
    //赋值给empMapper
    @Autowired
    private EmpMapper empMapper;

    @RequestMapping("/insert")
    public String insert(Emp emp){
        System.out.println("emp = " + emp);
        empMapper.insert(emp);
        return "添加成功";
    }
    @RequestMapping("/select")
    public String select(){
        List<Emp> emps = empMapper.select();
        String html = "<table border=1>";
        html+="<caption>员工列表</caption>";
        html+="<tr><td>id</td><td>名字</td><td>工作</td><td>工资</td><td>操作</td></tr>";
        //添加多行员工信息
        for(Emp e:emps){
            html+="<tr>";
            html+="<td>"+e.getId()+"</td>";
            html+="<td>"+e.getName()+"</td>";
            html+="<td>"+e.getJob()+"</td>";
            html+="<td>"+e.getSal()+"</td>";
            html+="<td><a href='/delete?id="+e.getId()+"'>删除</a></td>";
            html+="</tr>";
        }
        html+="</table>";
        return html;
    }
    @RequestMapping("/delete")
    public String delete(int id){
        System.out.println("id = " + id);
        empMapper.deleteById(id);
        return "<a href='/select'>返回员工信息列表</a>";
    }

    @RequestMapping("/update")
    public String update(Emp emp){
        System.out.println("emp = " + emp);
        empMapper.update(emp);
        return "<a href='/select'>返回员工信息列表</a>";
    }

}
